package com.kitten.kv.biz.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import com.kitten.framework.common.response.Response;
import com.kitten.kv.biz.domain.dataobject.CommentContentDO;
import com.kitten.kv.biz.domain.dataobject.CommentContentPrimaryKey;
import com.kitten.kv.biz.domain.repository.CommentContentRepository;
import com.kitten.kv.biz.service.CommentContentService;
import com.kitten.kv.dto.req.*;
import com.kitten.kv.dto.resp.FindCommentContentRspDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author kitten
 */
@Slf4j
@Service
public class CommentContentServiceImpl implements CommentContentService {
    @Resource
    private CassandraTemplate cassandraTemplate;

    @Resource
    private CommentContentRepository commentContentRepository;

    /**
     * 批量添加评论内容
     * @param reqDTO
     * @return
     */
    @Override
    public Response<?> batchAddCommentContent(BatchAddCommentContentReqDTO reqDTO) {
        List<CommentContentReqDTO> comments = reqDTO.getComments();

        List<CommentContentDO> contentDOS = comments.stream().map(comment -> {
            CommentContentPrimaryKey primaryKey = CommentContentPrimaryKey.builder()
                    .noteId(comment.getNoteId())
                    .yearMonth(comment.getYearMonth())
                    .contentId(UUID.fromString(comment.getContentId()))
                    .build();
            // DO实体类
            CommentContentDO contentDO = CommentContentDO.builder()
                    .primaryKey(primaryKey)
                    .content(comment.getContent())
                    .build();
            return contentDO;
        }).toList();
        // 批量插入
        cassandraTemplate.batchOps()
                .insert(contentDOS)
                .execute();
        return Response.success();
    }

    /**
     * 批量查询评论内容
     * @param batchFindCommentContentReqDTO
     * @return
     */
    @Override
    public Response<?> batchFindCommentContent(BatchFindCommentContentReqDTO batchFindCommentContentReqDTO) {
        // 归属的笔记 ID
        Long noteId = batchFindCommentContentReqDTO.getNoteId();

        // 查询评论的发布年月、内容 UUID
        List<FindCommentContentReqDTO> commentContentKeys = batchFindCommentContentReqDTO.getCommentContentKeys();

        // 过滤出年月
        List<String> yearMonths = commentContentKeys.stream()
                .map(FindCommentContentReqDTO::getYearMonth)
                .distinct() // 去重
                .collect(Collectors.toList());

        // 过滤出评论内容 UUID
        List<UUID> contentIds = commentContentKeys.stream()
                .map(commentContentKey -> UUID.fromString(commentContentKey.getContentId()))
                .distinct() // 去重
                .collect(Collectors.toList());

        // 批量查询 Cassandra
        List<CommentContentDO> commentContentDOS = commentContentRepository
                .findByPrimaryKeyNoteIdAndPrimaryKeyYearMonthInAndPrimaryKeyContentIdIn(noteId, yearMonths, contentIds);

        // DO 转 DTO
        List<FindCommentContentRspDTO> findCommentContentRspDTOS = Lists.newArrayList();
        if (CollUtil.isNotEmpty(commentContentDOS)) {
            findCommentContentRspDTOS = commentContentDOS.stream()
                    .map(commentContentDO -> FindCommentContentRspDTO.builder()
                            .contentId(String.valueOf(commentContentDO.getPrimaryKey().getContentId()))
                            .content(commentContentDO.getContent())
                            .build())
                    .toList();
        }

        return Response.success(findCommentContentRspDTOS);
    }

    /**
     * 删除评论内容
     * @param deleteCommentContentReqDTO
     * @return
     */
    @Override
    public Response<?> deleteCommentContent(DeleteCommentContentReqDTO deleteCommentContentReqDTO) {
        Long noteId = deleteCommentContentReqDTO.getNoteId();
        String yearMonth = deleteCommentContentReqDTO.getYearMonth();
        String contentId = deleteCommentContentReqDTO.getContentId();

        // 删除评论正文
        commentContentRepository.deleteByPrimaryKeyNoteIdAndPrimaryKeyYearMonthAndPrimaryKeyContentId(noteId, yearMonth, UUID.fromString(contentId));

        return Response.success();
    }
}
